#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using tl = tuple < int, int, int >;

const int N = 5e5 + 10;

int n, Q, a[N], b[N];

namespace sub1 {
	int sb[N], f[N];
	ll sa[N];
	void solve() {
		int l = in, r = in;
		rep(i, l, r) a[i - l + 1] = a[i], b[i - l + 1] = b[i];
		n = r - l + 1;
		sa[0] = sb[0] = 0; rep(i, 1, n) sa[i] = sa[i - 1] + a[i], sb[i] = sb[i - 1] + b[i];
		f[0] = 0;
		rep(i, 1, n) { f[i] = -n - 100;
			rep(j, 0, i - 1) {
				if(sb[i] - sb[j] == 1) f[i] = max(f[i], f[j] + (sa[i] - sa[j] >= 0));
			}
		} printf("%d\n", max(0, f[n]));
	}
}

void solve() {
	n = in, Q = in;
	rep(i, 1, n) a[i] = in;
	rep(i, 1, n) b[i] = in;
	if(Q == 1) sub1 :: solve();
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	int T = in; while(T--) solve();
	return 0;
}
